package com.wang.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    //时间转String
    public static String getStrDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = formatter.format(new Date());
        return dateTime;
    }


    //时间转String
    public static String dateToString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateTime = formatter.format(date);
        return dateTime;
    }

    //String转时间
    public static Date stringToDate(String Date, String format) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date date = formatter.parse(Date);
        return date;
    }
    //String转时间
    public static Long stringToLong(String Date) {

        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();

    }


    public static Integer differentDays(String sdate1, String sdate2) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        try {
            date1 = dateformat.parse(sdate1);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = dateformat.parse(sdate2);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //不同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //同一年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    public static String convert(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(Long.parseLong(date));

    }

    public static String Engconvert(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format((date));

    }

    //获取当前月份 年份 等
    public static Integer getMonth() {

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);

        return month;

    }

    //获取当前月份 年份 等
    public static Integer getYear() {

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);

        return year;

    }

    //判断今天周几  返回数字
    public static Integer getWeek() {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        return weekday;
    }

    //获取上周一
    public static String getLastMonday() {        // TODO Auto-generated method stub

        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(7 + (n - 1)));// 上周一的日期
        Date lastMonday = cal.getTime();

        String date = dateToString(lastMonday, "yyyy-MM-dd 00:00:00");

        return date;

    }

    //获取上周日
    public static String getLastSunday() {        // TODO Auto-generated method stub
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(7 + (n - 1)));// 上周一的日期
        cal.add(Calendar.DATE, 6);
        Date lastSunday = cal.getTime();
        return dateToString(lastSunday, "yyyy-MM-dd 23:59:59");
    }

    //获取本周一
    public static String getMonday() {
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        cal.add(Calendar.DATE, -(n - 1));// 本周一的日期
        Date monday = cal.getTime();

        String str = dateToString(monday, "yyyy-MM-dd 00:00:00");

        return str;
    }

    //获取当月1日
    public static String getMonthDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        String date = dateToString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
        return date;
    }

    //获取上月1日
    public static String getLastMonthDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        String date = dateToString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
        return date;
    }

    //获取上月最后一日
    public static String getLastMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);

        calendar.set(Calendar.MONTH, month - 1);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        String date = dateToString(calendar.getTime(), "yyyy-MM-dd 23:59:59");

        return date;
    }

    //获取3个月之前的日期
    public static String getLastThreeMonth() {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.MONTH, -3);  //设置为前3月
        dBefore = calendar.getTime();   //得到前3月的时间
        return dateToString(dBefore, "yyyy-MM-dd HH:mm:ss");
    }


    //获取昨天的时间
    public static String getYesterday() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        return yesterdayDate;
    }

    //获取当前日期+时间前10分钟
    public static String getTodayStartTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        //先获取今天日期
        String today = sdf3.format(new Date());

        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return today + " " + (sdf.format(date.getTime() - 600000));
    }

    //获取当前日期+时间后10分钟
    public static String getTodayEndTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        //先获取今天日期
        String today = sdf3.format(new Date());

        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return today + " " + (sdf.format(date.getTime() + 600000));
    }

    //获取昨天日期+时间前10分钟
    public static String getYesTodayStartTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        //先获取昨天日期
        String yesToday = getYesterday();

        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return yesToday + " " + (sdf.format(date.getTime() - 600000));
    }

    //获取昨天日期+时间后10分钟
    public static String getYesTodayEndTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        //先获取昨天日期
        String yesToday = getYesterday();

        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return yesToday + " " + (sdf.format(date.getTime() + 600000));
    }
    //获取每月 第一天

    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        return sdf.format(cal.getTime());
    }


    //计算两个时间的时间差 //返回天数
    public static String getTimeDifference(Date startTime, Date endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String resultTime = "";
        try {
            //Date d1 = df.parse("2004-03-26 13:31:40");
            //Date d2 = df.parse("2004-01-02 11:30:24");
            long diff = endTime.getTime() - startTime.getTime();//这样得到的差值是毫秒级别
            long days = diff / (1000 * 60 * 60 * 24);

            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            resultTime += days + "天" + hours + "小时" + minutes + "分";
        } catch (Exception e) {
        }

        return resultTime;

    }

    //计算两个时间的时间差 //返回天数
    public static String getTimeDifferenceHour(Date startTime, Date endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String resultTime = "";
        try {
            //Date d1 = df.parse("2004-03-26 13:31:40");
            //Date d2 = df.parse("2004-01-02 11:30:24");
            long diff = endTime.getTime() - startTime.getTime();//这样得到的差值是毫秒级别
            /*long days = diff / (1000 * 60 * 60 * 24);*/

            long hours = diff / (1000 * 60 * 60);
            /*long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);*/
            resultTime += String.valueOf(hours);
        } catch (Exception e) {
        }

        return resultTime;

    }


    public static Integer getHour(String date) {
        Date date2 = null;
        try {
            date2 = stringToDate(date, "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date2);
        Integer hour = cal.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    public static Integer getNowHour() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        Integer hour = cal.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

	/*public static String hourMiuToLong(String date){
		Date da = null;
		try {
			da = stringToDate(date,"HH:mm:ss");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = da.getTime();
		return ts;
	}*/

    //小时分钟  封装成yyyy-HH-mm HH:mm:ss
    public static Long packageDate(String hourDate) {
        //获取今天的日期
        String date1 = dateToString(new Date(), "yyyy-MM-dd");
        String newDate = date1 + " " + hourDate;
        Date da = null;

        try {
            da = stringToDate(newDate, "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return da.getTime();
    }

    //long转date
    public static Date longToDate(Long longDate) {
        Date date = new Date(longDate);
        return date;
    }

    //long转String
    public static String longToStringHour(Long longDate) {
        Date date = new Date(longDate);
        return dateToString(date, "HH:mm:ss");
    }


    public static void main(String[] args) {
        getPastDate(30);
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);

        return result;
    }


}
